package org.jeecg.modules.yusuan.controller;

import com.alibaba.druid.sql.visitor.functions.If;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.checkerframework.checker.units.qual.C;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.gudingzichan.entity.Gudingzichan;
import org.jeecg.modules.hqbaoxiao.entity.HqBaoxiao;
import org.jeecg.modules.hqbaoxiao.service.IHqBaoxiaoService;
import org.jeecg.modules.system.entity.SysDepart;
import org.jeecg.modules.system.model.TreeSelectModel;
import org.jeecg.modules.system.service.ISysDepartService;
import org.jeecg.modules.yusuan.entity.*;
import org.jeecg.modules.yusuan.entity.CaiwuZhijie;
import org.jeecg.modules.yusuan.mapper.CaiwuZhijieMapper;
import org.jeecg.modules.yusuan.service.ICaiwuBiaozhunService;
import org.jeecg.modules.yusuan.service.ICaiwuNzService;
import org.jeecg.modules.yusuan.service.ICaiwuZhijieService;
import org.jeecg.modules.yusuan.service.ICaiwuZhijieService;
import org.jeecg.modules.zhibiao.entity.CaiwuZhibiao;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
* @Description: caiwu_zhijie
* @Author: jeecg-boot
* @Date:   2020-06-15
* @Version: V1.0
*/
@Api(tags="caiwu_zhijie")
@RestController
@RequestMapping("/yusuan/zhijie")
@Slf4j
public class CaiwuZhijieController extends JeecgController<CaiwuZhijie, ICaiwuZhijieService> {
    @Autowired
    private ICaiwuNzService caiwuNzService;
   @Autowired
   private ICaiwuZhijieService caiwuZhijieService;
   @Autowired
   private ISysDepartService sysDepartService;
   @Autowired
   private IHqBaoxiaoService hqBaoxiaoService;
   @Autowired
   private ICaiwuBiaozhunService caiwuBiaozhunService;

   /**
    * 分页列表查询
    *
    * @param caiwuZhijie
    * @param pageNo
    * @param pageSize
    * @param req
    * @return
    */
   @AutoLog(value = "caiwu_zhijie-分页列表查询")
   @ApiOperation(value="caiwu_zhijie-分页列表查询", notes="caiwu_zhijie-分页列表查询")
   @GetMapping(value = "/list")
   public Result<?> queryPageList(CaiwuZhijie caiwuZhijie,
                                  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                  @RequestParam(name="deptId",required = false) String deptId,
                                  HttpServletRequest req) {
       pageNo = 1;
       pageSize = 1000;
       QueryWrapper<CaiwuZhijie> queryWrapper = new QueryWrapper<>(); // QueryGenerator.initQueryWrapper(caiwuZhijie, req.getParameterMap());
       if(StringUtils.isBlank(deptId)) {
           LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
           deptId = user.getOrgCode();
       }
       queryWrapper.eq("dept_id",deptId);
       Page<CaiwuZhijie> page = new Page<CaiwuZhijie>(pageNo, pageSize);
       IPage<CaiwuZhijie> pageList = caiwuZhijieService.page(page, queryWrapper);
       return Result.ok(pageList);
   }

   /**
    *   添加
    *
    * @return
    */
   @AutoLog(value = "caiwu_zhijie-添加")
   @ApiOperation(value="caiwu_zhijie-添加", notes="caiwu_zhijie-添加")
   @PostMapping(value = "/add")
   public Result<?> add(@RequestBody Map param) {
       LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
       String deptId = user.getOrgCode();
       List<Map> list = (List<Map>)param.get("list");
       if(!CollectionUtils.isEmpty(list)){
           List<CaiwuZhijie> yusuanList = new ArrayList<>();
           for(Map map:list){
               try {
                   map.remove("key");
                   map.remove("deptId_dictText");
                   ObjectMapper mapper = new ObjectMapper();
                   String jsonInString = mapper.writeValueAsString(map);
                   mapper = new ObjectMapper();
                   CaiwuZhijie yusuan = mapper.readValue(jsonInString, CaiwuZhijie.class);
                   yusuan.setDeptId(deptId);
                   Integer status = yusuan.getStatus();
                   if(null == status || status<1) {
                       yusuan.setStatus(-1);
                       yusuanList.add(yusuan);
                   }
               }catch (Exception ex){
                   ex.printStackTrace();
               }
           }
           caiwuZhijieService.saveYuSuanData(yusuanList);
       }

       return Result.ok("操作成功！");
   }

   @PostMapping("/addlist")
   public Result<?> addList(@RequestBody List<CaiwuZhijie> zhijieList){
       String orgCode = ((LoginUser) SecurityUtils.getSubject().getPrincipal()).getOrgCode();
       String deptId = this.sysDepartService.getOne(new LambdaQueryWrapper<SysDepart>().eq(SysDepart::getOrgCode,orgCode)).getId();
       if(org.apache.commons.collections.CollectionUtils.isNotEmpty(zhijieList)){
           zhijieList.forEach(zhijie->{
               if(StringUtils.isBlank(zhijie.getDeptId()))
                   zhijie.setDeptId(deptId);
           });
       }
       this.caiwuZhijieService.saveOrUpdateBatch(zhijieList);
       return Result.ok("操作成功");
   }

   /**
    *  编辑
    *
    * @param caiwuZhijie
    * @return
    */
   @AutoLog(value = "caiwu_zhijie-编辑")
   @ApiOperation(value="caiwu_zhijie-编辑", notes="caiwu_zhijie-编辑")
   @PutMapping(value = "/edit")
   public Result<?> edit(@RequestBody CaiwuZhijie caiwuZhijie) {
       caiwuZhijieService.updateById(caiwuZhijie);
       return Result.ok("编辑成功!");
   }

   /**
    *   通过id删除
    *
    * @param id
    * @return
    */
   @AutoLog(value = "caiwu_zhijie-通过id删除")
   @ApiOperation(value="caiwu_zhijie-通过id删除", notes="caiwu_zhijie-通过id删除")
   @DeleteMapping(value = "/delete")
   public Result<?> delete(@RequestParam(name="id",required=true) String id) {
       caiwuZhijieService.removeById(id);
       return Result.ok("删除成功!");
   }

   /**
    *  批量删除
    *
    * @param ids
    * @return
    */
   @AutoLog(value = "caiwu_zhijie-批量删除")
   @ApiOperation(value="caiwu_zhijie-批量删除", notes="caiwu_zhijie-批量删除")
   @DeleteMapping(value = "/deleteBatch")
   public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
       this.caiwuZhijieService.removeByIds(Arrays.asList(ids.split(",")));
       return Result.ok("批量删除成功!");
   }

   /**
    * 通过id查询
    *
    * @param id
    * @return
    */
   @AutoLog(value = "caiwu_zhijie-通过id查询")
   @ApiOperation(value="caiwu_zhijie-通过id查询", notes="caiwu_zhijie-通过id查询")
   @GetMapping(value = "/queryById")
   public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
       CaiwuZhijie caiwuZhijie = caiwuZhijieService.getById(id);
       if(caiwuZhijie==null) {
           return Result.error("未找到对应数据");
       }
       return Result.ok(caiwuZhijie);
   }

   /**
   * 导出excel
   *
   * @param request
   * @param caiwuZhijie
   */
   @RequestMapping(value = "/exportXls")
   public ModelAndView exportXls(HttpServletRequest request, CaiwuZhijie caiwuZhijie) {
       return super.exportXls(request, caiwuZhijie, CaiwuZhijie.class, "caiwu_zhijie");
   }

   /**
     * 通过excel导入数据
   *
   * @param request
   * @param response
   * @return
   */
   @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
   public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
       return super.importExcel(request, response, CaiwuZhijie.class);
   }

   @GetMapping("/nianduhuizong")
   public Result<?> nianduhuizong(@RequestParam(name="orgCode",required = false) String orgCode){
       if(StringUtils.isBlank(orgCode)){
           orgCode = ((LoginUser) SecurityUtils.getSubject().getPrincipal()).getOrgCode();
       }
       List<NianDuHuiZong> list = this.caiwuZhijieService.nianduhuizong(orgCode);
       return Result.ok(list);
   }

    /**
     * 加载一级节点/如果是同步 则所有数据
     */
    @RequestMapping(value = "/loadData", method = RequestMethod.GET)
    public Result<List<CaiwuZhijie>> loadTreeRoot(@RequestParam(name = "type")String type, @RequestParam( value = "deptId",required = false)String deptId,@RequestParam(name="pid") String pid) {
        if(StringUtils.isBlank(deptId)){
            String orgCode = ((LoginUser) SecurityUtils.getSubject().getPrincipal()).getOrgCode();
            deptId = this.sysDepartService.getOne(new LambdaQueryWrapper<SysDepart>().eq(SysDepart::getOrgCode,orgCode)).getId();
        }
        String deptCode = this.sysDepartService.getById(deptId).getOrgCode();
        Result<List<CaiwuZhijie>> result = new Result<List<CaiwuZhijie>>();
        try {
            Map<String, CaiwuZhijie> allData = new HashMap<>();
            List<CaiwuZhijie> leafList = new ArrayList<>();
            Set<CaiwuZhijie> leafSet = new HashSet<>(); //A01
            List<CaiwuZhijie> ls = this.caiwuZhijieService.listData(deptId,deptCode,pid);
            loadDataChildren(leafList,allData,deptId,deptCode,ls);
            if(!CollectionUtils.isEmpty(leafList)){
                leafList.forEach(item->{
                    String xiafashu = item.getHjshenqingshu();
                    String orgCode = item.getSysOrgCode();

                    if(!StringUtils.equalsIgnoreCase(orgCode,"A01")){
                        xiafashu = allData.get(item.getParentId()).getHjshenqingshu();
                        leafSet.add(allData.get(item.getParentId()));
                    }else{
                        leafSet.add(item);
                    }
                    xiafashu = null == xiafashu?"0":xiafashu;
//                    item.setMaxNum(xiafashu);
                    if(Double.parseDouble(xiafashu)>0){
                        item.setDisplay(true);
                    }
                });
                leafSet.forEach(item->{
                    String xiafashu = item.getHjshenqingshu();
                    xiafashu = null == xiafashu?"0":xiafashu;
//                    item.setMaxNum(xiafashu);
                    if(Double.parseDouble(xiafashu)>0){
                        item.setDisplay(true);
                        iterParent(allData,item,item);
                    }
                });
            }
            CaiwuZhijie total = new CaiwuZhijie();
            BeanUtils.copyProperties(ls.get(0),total);
            total.setIsLeaf(true);
            total.setChildren(null);
            total.setCategoryId("0");
            total.setCategoryName("合计");
            ls.add(0,total);
            result.setResult(ls);
            result.setSuccess(true);
            if(StringUtils.equals(type,"shenpi") && StringUtils.isNotBlank(total.getHjshenheshu()) && Double.parseDouble(total.getHjshenheshu())>0){
                List<CaiwuNz> nzList = caiwuNzService.list(new LambdaQueryWrapper<CaiwuNz>().eq(CaiwuNz::getDeptId,deptId).eq(CaiwuNz::getJingfeiType,"zhijie"));
                if(CollectionUtils.isEmpty(nzList)){
                    CaiwuNz caiwuNz = new CaiwuNz();
                    caiwuNz.setXiafashu("0.00");
                    caiwuNz.setShenqingshu(total.getHjshenqingshu());
                    caiwuNz.setShenheshu(total.getHjshenheshu());
                    caiwuNz.setZengjian(total.getHjzengjian());
                    caiwuNz.setDeptId(deptId);
                    caiwuNz.setJingfeiType("biaozhun");
                    caiwuNzService.save(caiwuNz);
                }else {
                    CaiwuNz caiwuNz = nzList.get(0);
                    if(StringUtils.isBlank(caiwuNz.getProcessInstanceId())){
                        caiwuNz.setXiafashu("0.00");
                        caiwuNz.setShenqingshu(total.getHjshenqingshu());
                        caiwuNz.setShenheshu(total.getHjshenheshu());
                        caiwuNz.setZengjian(total.getHjzengjian());
                        caiwuNzService.updateById(caiwuNz);
                    }
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
            result.setMessage(e.getMessage());
            result.setSuccess(false);
        }
        result.setMessage(deptId);
        return result;
    }

    private void iterParent(Map<String,CaiwuZhijie> allData,CaiwuZhijie biaozhun,CaiwuZhijie leafItem){
        String categoryId = biaozhun.getParentId();
        CaiwuZhijie parent =allData.get(categoryId);
        if(null == parent){
            return;
        }
        parent.setDisplay(true);
        if(!StringUtils.equals(parent.getId(),"1270528547133616130")){
            if(StringUtils.equalsIgnoreCase(biaozhun.getSysOrgCode(),"A01")) {
                String shenqingshu = parent.getHjshenqingshu();
                shenqingshu = null == shenqingshu ? "0" : shenqingshu;
                String sqs = leafItem.getHjshenqingshu();
                sqs = null == sqs ? "0" : sqs;
                parent.setHjshenqingshu((Double.parseDouble(shenqingshu) + Double.parseDouble(sqs)) + "");

                String shenheshu = parent.getHjshenheshu();
                shenheshu = null == shenheshu ? "0" : shenheshu;
                String shs = leafItem.getHjshenheshu();
                shs = null == shs ? "0" : shs;
                parent.setHjshenheshu((Double.parseDouble(shenheshu) + Double.parseDouble(shs)) + "");

                String zengjian = parent.getHjzengjian();
                zengjian = null == zengjian?"0":zengjian;
                String zj = leafItem.getHjzengjian();
                zj = null == zj?"0":zj;
                parent.setHjzengjian((Double.parseDouble(zengjian) + Double.parseDouble(zj)) + "");
            }
            iterParent(allData,parent,leafItem);
        }
    }

    /**
     * 递归求子节点 同步加载用到
     */
    private void loadDataChildren(List<CaiwuZhijie> leafList,Map<String,CaiwuZhijie> allData,String deptId,String deptCode,List<CaiwuZhijie> ls) {
        for (CaiwuZhijie tsm : ls) {
            allData.put(tsm.getTreeId(),tsm);
            List<CaiwuZhijie> temp = this.caiwuZhijieService.listData(deptId,deptCode,tsm.getTreeId());
            if(temp!=null && temp.size()>0) {
                tsm.setChildren(temp);
                loadDataChildren(leafList,allData,deptId,deptCode,temp);
            }else{
                leafList.add(tsm);
            }
        }
    }

    @GetMapping("/getbycateanddept")
    public Result<?> getByCateAndDept(@RequestParam String categoryId){
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        String orgCode = user.getOrgCode();

        String deptId = this.sysDepartService.getOne(new LambdaQueryWrapper<SysDepart>().eq(SysDepart::getOrgCode,orgCode), false).getId();
        CaiwuZhijie zhijie = this.caiwuZhijieService.getOne(new LambdaQueryWrapper<CaiwuZhijie>().eq(CaiwuZhijie::getDeptId,deptId).eq(CaiwuZhijie::getCategoryId,categoryId));
//        String hjshenheshu = null == zhijie?"0":zhijie.getHjshenheshu();
        String hjshenheshu="";
        if (zhijie.getHjshenheshu()==null||zhijie.getHjshenheshu()==""){
            hjshenheshu="0";
        }else {
            hjshenheshu=zhijie.getHjshenheshu();
        }
        Double amount = Double.parseDouble(hjshenheshu);

        List<HqBaoxiao> list = this.hqBaoxiaoService.list(new LambdaQueryWrapper<HqBaoxiao>().eq(HqBaoxiao::getBaoxStatus,1).eq(HqBaoxiao::getSysOrgCode,orgCode));
        if(!CollectionUtils.isEmpty(list)){
            for(HqBaoxiao item:list){
                if (item.getHezje()==null||item.getHezje()==""){
                    item.setHezje("0.00");
                }
                amount -= Double.parseDouble(item.getHezje());
            }
        }
        return Result.ok(amount);
    }

    public void add2Map(CaiwuZhijie caiwuZhijie,Map<String,CaiwuZhijie> map){
        map.put(caiwuZhijie.getTreeId(),caiwuZhijie);
        List<CaiwuZhijie> ls = caiwuZhijie.getChildren();
        if(null != ls && ls.size()>0){
            ls.forEach(caiwuZhijie1 -> {
                add2Map(caiwuZhijie1,map);
            });
        }
    }
    /**
     * 加载一级节点/如果是同步 则所有数据
     */
    @RequestMapping(value = "/jfcx", method = RequestMethod.GET)
    public Result<List<CaiwuZhijie>> jfcx(@RequestParam( value = "deptId",required = false)String deptId,@RequestParam(name="pid") String pid) {

        if(StringUtils.isBlank(deptId)){
            String orgCode = ((LoginUser) SecurityUtils.getSubject().getPrincipal()).getOrgCode();
            deptId = this.sysDepartService.getOne(new LambdaQueryWrapper<SysDepart>().eq(SysDepart::getOrgCode,orgCode)).getId();
        }
        String orgCode = this.sysDepartService.getOne(new LambdaQueryWrapper<SysDepart>().eq(SysDepart::getId,deptId)).getOrgCode();

        List<NianDuHuiZong> nianDuHuiZongList = this.caiwuBiaozhunService.nianduhuizong(orgCode);

        Map<String,NianDuHuiZong> nianDuHuiZongMap = this.caiwuBiaozhunService.nianduhuizongData(nianDuHuiZongList);

        Map<String,CaiwuZhijie> tmpMap = new HashMap();
        Result<List<CaiwuZhijie>> result = new Result<List<CaiwuZhijie>>();
        try {
            Map<String, CaiwuZhijie> allData = new HashMap<>();
            List<CaiwuZhijie> leafList = new ArrayList<>();
            List<CaiwuZhijie> ls = this.caiwuZhijieService.listData(deptId,orgCode,pid);
            loadDataChildren(leafList,allData,deptId,orgCode,ls);

            ls.forEach(caiwuZhijie -> {
                add2Map(caiwuZhijie,tmpMap);
            });
            List<Map> list = ((CaiwuZhijieMapper)(this.caiwuZhijieService.getBaseMapper())).hejbaoxiao(orgCode);
            if(!CollectionUtils.isEmpty(list)){
                list.forEach(map -> {
                    CaiwuZhijie j = tmpMap.get((String)map.get("yuskm"));
                    if(j!= null)
                        j.setBaoxiaoshu("" + map.get("baoxiaoshu"));
                });
            }

            if(null != nianDuHuiZongMap){
                Iterator<String> iterator = nianDuHuiZongMap.keySet().iterator();
                while (iterator.hasNext()){
                    String km = iterator.next();
                    NianDuHuiZong ndhz = nianDuHuiZongMap.get(km);
                    CaiwuZhijie j = tmpMap.get(km);
                    if(j!= null){
                        String zcBiaozhun = ndhz.getZcBiaozhun();
                        String zcXiangmu = ndhz.getZcXiangmu();
                        String zcZhijie = ndhz.getZcZhijie();
                        zcBiaozhun = null == zcBiaozhun?"0":zcBiaozhun;
                        zcXiangmu = null == zcXiangmu?"0":zcXiangmu;
                        zcZhijie = null == zcZhijie?"0":zcZhijie;
                        j.setZchj("" + (Double.parseDouble(zcBiaozhun) + Double.parseDouble(zcXiangmu) + Double.parseDouble(zcZhijie)));
                    }
                }
            }

            if(!CollectionUtils.isEmpty(leafList)){
                leafList.forEach(item->{
                    String xiafashu = item.getHjshenqingshu();
                    String code = item.getSysOrgCode();

                    if(!StringUtils.equalsIgnoreCase(code,"A01")){
                        xiafashu = allData.get(item.getParentId()).getHjshenqingshu();
                    }
                    xiafashu = null == xiafashu?"0":xiafashu;

                    if(Double.parseDouble(xiafashu)>0){
                        item.setDisplay(true);
                        iterParent(allData,item,item);
                    }
                });
            }
            CaiwuZhijie total = new CaiwuZhijie();
            BeanUtils.copyProperties(ls.get(0),total);
            total.setIsLeaf(true);
            total.setChildren(null);
            total.setCategoryId("0");
            total.setCategoryName("合计");
            ls.add(0,total);

            result.setResult(ls);
            result.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
            result.setMessage(e.getMessage());
            result.setSuccess(false);
        }
        return result;
    }

    @RequestMapping(value = "/zxl", method = RequestMethod.GET)
    public Result<?> zxl() {

        List<Map<String, Object>> result = new ArrayList<>();
        // val 部门表根节点的id
        List<SysDepart> list = sysDepartService.list(new LambdaQueryWrapper<SysDepart>().eq(SysDepart::getParentId, "1331571789934395393"));
        list.stream().forEach(item -> {
            CaiwuZhijie zhijie = getDeptZxl(item.getId(),item.getOrgCode());
            if(StringUtils.isBlank(zhijie.getBaoxiaoshu())){
                zhijie.setBaoxiaoshu("0");
            }
            if(StringUtils.isBlank(zhijie.getHjshenheshu())){
                zhijie.setHjshenheshu("1");
            }
            Map<String, Object> map = new HashMap<>();
            map.put("bummc", item.getDepartName());
            map.put("santybmbm", item.getOrgCode2());
            map.put("count", String.format("%.2f", Double.parseDouble(zhijie.getBaoxiaoshu())/Double.parseDouble(zhijie.getHjshenheshu())));
            result.add(map);
        });

        return Result.ok(result);
    }

    private CaiwuZhijie getDeptZxl(String deptId,String orgCode) {
        String pid = "1270528547133616130";
        CaiwuZhijie ret = new CaiwuZhijie();
        Map<String,CaiwuZhijie> tmpMap = new HashMap();
        try {
            Map<String, CaiwuZhijie> allData = new HashMap<>();
            List<CaiwuZhijie> leafList = new ArrayList<>();
            List<CaiwuZhijie> ls = this.caiwuZhijieService.listData(deptId, orgCode, pid);
            loadDataChildren(leafList, allData, deptId, orgCode, ls);

            ls.forEach(caiwuZhijie -> {
                add2Map(caiwuZhijie, tmpMap);
            });
            List<Map> list = ((CaiwuZhijieMapper) (this.caiwuZhijieService.getBaseMapper())).hejbaoxiao(orgCode);
            Double hjBaoxiaoshu = 0D;
            if (!CollectionUtils.isEmpty(list)) {
                for (Map map : list) {
                    CaiwuZhijie j = tmpMap.get((String) map.get("yuskm"));
                    if (j != null) {
                        j.setBaoxiaoshu("" + map.get("baoxiaoshu"));
                    } else {
                        j.setBaoxiaoshu("");
                    }
                    if (StringUtils.isNotBlank(j.getBaoxiaoshu())) {
                        hjBaoxiaoshu += Double.parseDouble(j.getBaoxiaoshu());
                    }
                }
            }

            if (!CollectionUtils.isEmpty(leafList)) {
                leafList.forEach(item -> {
                    String xiafashu = item.getHjshenqingshu();
                    String code = item.getSysOrgCode();

                    if (!StringUtils.equalsIgnoreCase(code, "A01")) {
                        xiafashu = allData.get(item.getParentId()).getHjshenqingshu();
                    }
                    xiafashu = null == xiafashu ? "0" : xiafashu;

                    if (Double.parseDouble(xiafashu) > 0) {
                        item.setDisplay(true);
                        iterParent(allData, item, item);
                    }
                });
            }
            ret = ls.get(0);
            ret.setBaoxiaoshu(Double.toString(hjBaoxiaoshu));
        }catch (Exception ex) {
            ex.printStackTrace();
            ret.setHjshenheshu("1");
            ret.setBaoxiaoshu("0");
        }
        return ret;
    }
}
